package com.cs.framework.web.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import com.cs.framework.common.util.JSONUtils;
import com.cs.framework.common.util.StringUtils;

/**
 * 请求工具
 * 
 * @author hedongzhou
 *
 */
@SuppressWarnings("all")
public class RequestUtils {

	/**
	 * 获取JSON字符串
	 * 
	 * @param request
	 * @return
	 */
	public static String getJSONString(ServletRequest request) {
		BufferedReader reader = null;
		try {
			reader = request.getReader();

			return JSONUtils.toString(reader);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
				}
			}
		}
	}

	/**
	 * 获取请求参数Map
	 * 
	 * @param request
	 * @return
	 */
	public static Map getParameterMap(ServletRequest request) {
		Map properties = request.getParameterMap();
		Map returnMap = new HashMap();

		Iterator entries = properties.entrySet().iterator();
		Map.Entry entry = null;
		String name = null;
		String value = null;
		while (entries.hasNext()) {
			entry = (Map.Entry) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();

			if (null == valueObj) {
				value = "";
			} else if (valueObj instanceof String[]) {
				String[] values = (String[]) valueObj;
				for (int i = 0; i < values.length; i++) {
					value = values[i] + ",";
				}
				value = value.substring(0, value.length() - 1);
			} else {
				value = valueObj.toString();
			}

			returnMap.put(name, value);
		}

		return returnMap;
	}

	/**
	 * 判断是否为Ajax请求
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isAjax(HttpServletRequest request) {
		String requestType = request.getHeader("X-Requested-With");

		if (StringUtils.equals(requestType, "XMLHttpRequest")) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取请求路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getUrl(HttpServletRequest request) {
		return request.getRequestURI();
	}

	/**
	 * 获取根路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getRoot(HttpServletRequest request) {
		return request.getContextPath();
	}

	/**
	 * 获取资源路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getResource(HttpServletRequest request) {
		return StringUtils.removeStart(getUrl(request), getRoot(request));
	}

	/**
	 * 获取资源路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getResourceNoSuffix(HttpServletRequest request) {
		return noSuffix(getResource(request));
	}

	/**
	 * 判断是否存在后缀
	 * 
	 * @param url
	 * @return
	 */
	public static boolean hasSuffix(String url) {
		return url.indexOf(".") != -1;
	}

	/**
	 * 判断是否存在后缀
	 * 
	 * @param request
	 * @return
	 */
	public static boolean hasSuffix(HttpServletRequest request) {
		return hasSuffix(request.getRequestURI());
	}

	/**
	 * 去除后缀
	 * 
	 * @param url
	 * @return
	 */
	public static String noSuffix(String url) {
		return hasSuffix(url) ? url.substring(0, url.indexOf(".")) : url;
	}

	/**
	 * 去除后缀
	 * 
	 * @param request
	 * @return
	 */
	public static String noSuffix(HttpServletRequest request) {
		return noSuffix(getUrl(request));
	}

	/**
	 * 获取后缀
	 * 
	 * @param url
	 * @return
	 */
	public static String getSuffix(String url) {
		return hasSuffix(url) ? url.substring(url.indexOf(".")) : "";
	}

	/**
	 * 获取后缀
	 * 
	 * @param request
	 * @return
	 */
	public static String getSuffix(HttpServletRequest request) {
		return getSuffix(getUrl(request));
	}

}
